<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>购物车</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" href="amazeui/css/amazeui.min.css"/>
<link rel="stylesheet" href="default/style.css"/>
<script src="amazeui/js/jquery.min.js"></script>
<script src="amazeui/js/amazeui.min.js"></script>
    <script>
        function checkIsInteger(str)
        {
            //如果为空，则通过校验
            if(str == "")
                return true;
            if(/^(\-?)(\d+)$/.test(str))
                return true;
            else
                return false;
        }
        var errordialog=function(c){
            alert(c)
        };
        //检验商品数量
        function checkCounts(id) {
            var Counts = $("#" + id).val();
            if (Counts == "") {
                errordialog("请填写数量!");
                return false;
            }
            else if (!checkIsInteger(Counts)) {
                errordialog("商品数量不是整数!");
                return false;
            }
            else if (Counts < 1) {
                errordialog("商品数量不能小于1!");
                return false;
            }
            else {
                return true;
            }
        }
        function addQty(obj){
            // checkCounts('txtQty');
            var f=$(obj).parent("p").find("input");
            var qty = parseInt($(f).val());
            if(isNaN(qty)){
                qty=0;
            }
            $(f).val(qty+1);
            jisuan();
        }
        function subtractQty(obj){
            var f=$(obj).parent("p").find("input");
            // checkCounts('txtQty');
            var qty = parseInt($(f).val());
            if(isNaN(qty)){
                qty=2;
            }
            if(qty <=1){
                errordialog("商品数量不能小于1");
                return;
            }
            $(f).val(qty-1);
            jisuan();
        }
        function jisuan() {
            var total=0;
            $(".qtyI").each(function (index,element) {
                var price=$(this).parent("p").find(".priceS").text();
                price=Number(price);
                var f=$(this).parent("p").find("input");
                var qty = parseInt($(f).val());
                total=total+qty*price;
            });
            $("#total_amount").text(total);
        }
    </script>
</head>

<body>
<div class="container">
	<header data-am-widget="header" class="am-header am-header-default my-header">
      <!--<div class="am-header-left am-header-nav">-->
        <!--<a href="#left-link" class="">-->
          <!--<i class="am-header-icon am-icon-chevron-left"></i>-->
        <!--</a>-->
      <!--</div>-->
      <h1 class="am-header-title">
        <a href="#title-link" class="">购物车</a>
      </h1>
      <!--<div class="am-header-right am-header-nav">-->
        <!--<a href="#right-link" class="">-->
          <!--<i class="am-header-icon  am-icon-home"></i>-->
        <!--</a>-->
      <!--</div>-->
    </header>
    <div class="am-cf cart-panel buy-car-list" th:each="list : ${products} " th:d-id="${list.id}">
    	<div class="cart-list-panel" style="padding-bottom: 4%">
            <ul class="am-avg-sm-1 cart-panel-ul">
                <li>
                	<div class="imgpanel"><a href="#"><img th:src="${list.product.img}" class="am-img-responsive" /></a></div>
                	<div class="infopanel" style="width: 60%;float: inherit;padding-top: 8px;padding-bottom: 2px;">
                    	<h3><a href="#" th:text="${list.product.name}">商品名称</a></h3>
                        <p th:text="${'规格:'+list.attrStr}">规格</p>
                        <p class="priceP">价格：<span class="red2 bold priceS" th:text="${list.product.price}">100.00</span> 元  X
                            <button type="button" class="am-btn am-btn-default" style="padding: inherit" onClick="subtractQty(this);" ><i class="am-icon-minus"></i></button>
                            <input class="am-input-sm txt-qty qtyI" type="number" th:value="${list.qty}" />
                            <button type="button" class="am-btn am-btn-default" style="padding: inherit" onClick="addQty(this);"><i class="am-icon-plus"></i></button>
                        </p>
                        <!--<p>积分：<span class="red2 bold">78.00</span></p>-->
                        <p style="margin-top: 2%;cursor: pointer;" class="delete-button" ><span class="am-fr "><a class="am-badge am-badge-danger am-round">删除</a></span></p>
                    </div>
                </li>
            </ul>
        </div>
    </div>
    <div style="padding-left:65%">总价：<span class="red2 bold" id="total_amount">100</span>元</div>
    <div class="cart-tool" id="ok_buy">
        <a class="am-btn am-btn-sm am-btn-warning" href="#">
          <i class="am-icon-shopping-cart"></i>
          结账
        </a>
    </div>


    <footer data-am-widget="footer" class="am-footer am-footer-default" data-am-footer="{  }">
        <hr data-am-widget="divider" style="" class="am-divider am-divider-default"/>
        <div class="am-footer-miscs ">
            <p>广东温氏乳业有限公司</p>

        </div>
    </footer>
    <!--底部-->
    <div data-am-widget="navbar" class="am-navbar am-cf my-nav-footer " id="">
        <ul class="am-navbar-nav am-cf am-avg-sm-4 my-footer-ul">
            <li>
                <a href="/index.html" class="">
                    <span class="am-icon-home"></span>
                    <span class="am-navbar-label">首页</span>
                </a>
            </li>
            <li>
                <a href="/shoppingcart.html" class="">
                    <span class=" am-icon-cart-plus"></span>
                    <span class="am-navbar-label">购物车</span>
                </a>
            </li>
            <li style="position:relative">
                <a href="/reg.html" class="">
                    <span class="am-icon-user"></span>
                    <span class="am-navbar-label">收货地址</span>
                </a>
            </li>
            <li style="position:relative">
                <a href="/groupbuy.html" class="">
                    <span class="am-icon-list"></span>
                    <span class="am-navbar-label">订单列表</span>
                </a>
            </li>
        </ul>
        <script>
            function showFooterNav(){
                $("#footNav").toggle();
            }
        </script>
    </div>
</div>
</body>
<script src="public.js"></script>
<script>
    jisuan();
    var deleteing=false;
    var my_orderId=0;
    function onBridgeReady(data){
        WeixinJSBridge.invoke(
            // 'getBrandWCPayRequest', {
            //     "appId":"wx2421b1c4370ec43b",     //公众号名称，由商户传入
            //     "timeStamp":"1395712654",         //时间戳，自1970年以来的秒数
            //     "nonceStr":"e61463f8efa94090b1f366cccfbbb444", //随机串
            //     "package":"prepay_id=u802345jgfjsdfgsdg888",
            //     "signType":"MD5",         //微信签名方式：
            //     "paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
            // },
            'getBrandWCPayRequest',data,
            function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok" ){
                    // alert("支付成功");
                    window.location.href="/orderdetail.html?id="+my_orderId;
                    // 使用以上方式判断前端返回,微信团队郑重提示：
                    //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                }else if(res.err_msg == "get_brand_wcpay_request:fail"){
                    alert("支付失败");
                }
            });
    }
    $(document).off("click",".delete-button").on("click",".delete-button",function () {
        var id=$(this).parents(".buy-car-list").attr("d-id");
        var obj=$(this).parents(".buy-car-list");
        if(deleteing){
            return;
        }
        deleteing=true;
        $.ajax({
            type: 'POST',
            url: '/verify/deleteBuyCar',
            data: {id:parseInt(id)},
            dataType:"JSON",
            success: function (data) {
                console.log(data);
                if(data.code==0){
                    $(obj).remove();
                    jisuan();
                    // alert("操作成功!");
                }else{
                    alert(data.msg);
                }
                deleteing=false;
                return;
            },
            error:function () {
                deleteing=false;
                alert("服务器出错，请稍后再试");
                return;
            }
        });
    });
    $(".qtyI").on("input propertychange",function () {
        jisuan();
    });
    $("#ok_buy").click(function () {
        var total=parseInt($("#total_amount").text());
        console.log(total);
        if(total<5000){
            // alert("团购最低5000起!");
            // return;
        }
        var postObj={};
        $(".buy-car-list").each(function () {
            var id=$(this).attr("d-id");
            var qty=$(this).find(".qtyI").val();
            postObj[id]=qty;
        });
        $.ajax({
            type: 'POST',
            url: '/verify/buy',
            data: {obj:JSON.stringify(postObj)},
            dataType:"JSON",
            success: function (data) {
                console.log(data);
                if(data.code==0){
                    var pay=data.data;
                    console.log(pay);
                    my_orderId=pay['my_orderId'];
                    delete pay['my_orderId'];
                    // var pay={
                    //
                    // };
                    if (typeof WeixinJSBridge == "undefined"){
                        if( document.addEventListener ){
                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady(pay), false);
                        }else if (document.attachEvent){
                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady(pay));
                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(pay));
                        }
                    }else{
                        onBridgeReady(pay);
                    }
                    // alert("操作成功,请耐心等待工作人员联系。");
                    // window.location.reload();
                    // alert("操作成功!");
                }else if(data.code==-3){
                    alert("请先登记收货信息");
                    window.location.href="/reg.html";
                }else{
                    alert(data.msg);
                }
                return;
            },
            error:function () {
                alert("服务器出错，请稍后再试");
                return;
            }
        });
    });
</script>
</html>
